#include <modules\graphics\opengl\opengl_shader.h>

namespace DelGE
{
	namespace Modules
	{
		namespace Graphics
		{
			OpenGLShader::OpenGLShader()
			{
				this->eShaderType = ShaderType_Vertex;
				this->iShaderID = 0;
			}

			OpenGLShader::OpenGLShader(ShaderType eShaderType, string sShaderSource)
			{
				const char *pShaderSource = sShaderSource.c_str();

				this->eShaderType = eShaderType;
				this->iShaderID = glCreateShader(OpenGLShader::GLShaderType(eShaderType));
				
				glShaderSource(this->iShaderID, 1, &pShaderSource, NULL);
			}

			OpenGLShader::~OpenGLShader()
			{
				glDeleteShader(this->iShaderID);
			}

			OpenGLShader *OpenGLShader::CreateShaderFromSource(ShaderType eShaderType, string sShaderSource)
			{
				return new OpenGLShader(eShaderType, sShaderSource);
			}

			OpenGLShader *OpenGLShader::CreateShaderFromFile(ShaderType eShaderType, string sFilepath)
			{
				string source;
				ifstream file;

				file.open(sFilepath, ifstream::in);

				if(file)
				{
					string line;
					while (getline(file, line))
						source += line + "\n";
				}

				file.close();

				return new OpenGLShader(eShaderType, source);
			}

			ShaderType OpenGLShader::GetShaderType()
			{
				return this->eShaderType;
			}

			int OpenGLShader::GetShaderID()
			{
				return this->iShaderID;
			}

			void OpenGLShader::Compile()
			{
				glCompileShader(this->iShaderID);
			}

			GLenum OpenGLShader::GLShaderType(ShaderType eShaderType)
			{
				switch (eShaderType)
				{
				case ShaderType_Vertex:
					return GL_VERTEX_SHADER;

				case ShaderType_Fragment:
					return GL_FRAGMENT_SHADER;
				}

				return 0;
			}
		}
	}
}